const path = require("path");
const Multer = require("koa-multer");
const Jimp = require("jimp");
const { logger } = require("../../logger");

const { AVATAR_PATH, PICTURE_PATH } = require("../constans/file-path");

const avatarUpload = Multer({
  dest: AVATAR_PATH,
});
const avatarHandler = avatarUpload.single("avatar");

const pictureUpload = Multer({
  dest: PICTURE_PATH,
});
const pictureHandler = pictureUpload.array("picture", 9); //9是最大上传数

const pictureResize = async (ctx, next) => {
  try {
    // 1.获取所有的图像信息
    const files = ctx.req.files;
    // 2.对图像进行处理
    for (let file of files) {
      const destPath = path.join(file.destination, file.originalname);
      Jimp.read(file.path)
        .then((image) => {
          //   image.resize(1280, Jimp.AUTO).write(`${destPath}-large`);
          //   image.resize(640, Jimp.AUTO).write(`${destPath}-middle`);
          //   image.resize(320, Jimp.AUTO).write(`${destPath}-small`);
          image.resize(640, Jimp.AUTO).write(`${destPath}`);
        })
        .catch((err) => {
          console.log(err);
        });
    }
  } catch (error) {
    logger.error(error);
  }

  await next();
};

module.exports = {
  avatarHandler,
  pictureHandler,
  pictureResize,
};
